@echo off
setlocal EnableDelayedExpansion
set DEBUG=TRUE
if "%1"=="" call:help&goto:eof
if "%1"=="restartAdb" call:restartAdb&goto:eof
if "%1"=="taskInfo" (for /f "skip=1 usebackq" %%i in (`adb devices`) do call:param1 %%i %1&echo,)&goto:eof
if "%1"=="topInfo" (for /f "skip=1 usebackq" %%i in (`adb devices`) do call:topInfo %%i %2&echo,)&goto:eof
if "%2"=="" call:help&goto:eof
for /f "skip=1 usebackq" %%i in (`adb devices`) do call:deviceInfo %%i&call:%1 %%i %2&echo,
if not %errorlevel%==0 call:help
goto:eof

:param1
::param deviceId
::param cmd
call:deviceInfo %%1&call:%2 %1
goto:eof

:start
rem param deviceId
rem param packageName
call:_isInstall %1 %2
if not "!_isInstall!"=="TRUE" echo %2 isn't install on this device&goto:eof
call:launcherComponent %1 %2
if not exist %2.launcherComponent echo error:can't get launcherComponent.&goto:eof
for /f %%i in (%2.launcherComponent) do adb -s %1 shell am start-activity %%i
goto:eof

:startWeb
rem param deviceId
rem param webUrl
adb -s %1 shell am start-activity -a android.intent.action.VIEW -d %2
goto:eof


:input
rem param deviceId
rem param text
adb -s %1 shell input text %2
goto:eof


:kill
rem param deviceId
rem param packageName
call:_isInstall %1 %2
if not "!_isInstall!"=="TRUE" echo %2 isn't install on this device&goto:eof
adb -s %1 shell am force-stop %2
goto:eof

:restart
rem param deviceId
rem param packageName
call:_isInstall %1 %2
if not "!_isInstall!"=="TRUE" echo %2 isn't install on this device&goto:eof
call:kill %1 %2
call:start %1 %2
goto:eof

:clear
rem param deviceId
rem param packageName
call:_isInstall %1 %2
if not "!_isInstall!"=="TRUE" echo %2 isn't install on this device&goto:eof
adb -s %1 shell pm clear %2
goto:eof

:clearAndRestart
rem param deviceId
rem param packageName
call:_isInstall %1 %2
if not "!_isInstall!"=="TRUE" echo %2 isn't install on this device&goto:eof
call:clear %1 %2
call:start %1 %2
goto:eof

:details
rem param deviceId
rem param packageName
call:_isInstall %1 %2
if not "!_isInstall!"=="TRUE" echo %2 isn't install on this device&goto:eof
adb -s %1 shell am start-activity -a android.settings.APPLICATION_DETAILS_SETTINGS -d package:%2
goto:eof

:taskInfo
rem param devicesNo
rem param packName
adb -s %1 shell dumpsys activity r|findstr /c:"Recent #">actInfo.tmp
for /f "tokens=6" %%i in (actInfo.tmp) do set PACKAGE_NAME=%%i&&echo     !PACKAGE_NAME:~2!
goto:eof

:topInfo
rem param devicesNo
rem param packName
call:deviceInfo %1
adb -s %1 shell dumpsys activity r|findstr /c:"Recent #0:">actInfo.tmp
for /f "tokens=6" %%i in (actInfo.tmp) do set PACKAGE_NAME=%%i
if "%2"=="" (set dstPackage=!PACKAGE_NAME:~2!) else (set dstPackage=%2)
adb -s %1 shell dumpsys "activity top|grep ACTIVITY|grep !dstPackage!">actInfo.tmp
for /f "tokens=2" %%i in (actInfo.tmp) do echo component: %%i&adb -s %1 shell dumpsys activity %%i|findstr "#[0-9]*:"|findstr /v "android.arch.lifecycle.LifecycleDispatcher.report_fragment_tag com.bumptech.glide.manager androidx.lifecycle.LifecycleDispatcher.report_fragment_tag"|findstr /v "Msg #[0-9]*:">actInfo.tmp
type actInfo.tmp
del actInfo.tmp
goto:eof

:restartAdb
for /f "skip=1 usebackq tokens=2,5" %%i in (`netstat -aon`) do @if "%%i"=="127.0.0.1:5037" if not "%%j"=="0" taskkill /pid:%%j /f 1>nul 2>nul
taskkill /im:adb.exe /f 1>nul 2>nul
adb devices 1>nul 2>nul
echo !time! restart adb finish.
goto:eof

:uninstall
rem param deviceId
rem param packageName
call:_isInstall %1 %2
if not "!_isInstall!"=="TRUE" echo %2 isn't install on this device&goto:eof
adb -s %1 uninstall %2
goto:eof

:revokePermission
rem param deviceId
rem param packageName
call:_isInstall %1 %2
if not "!_isInstall!"=="TRUE" echo %2 isn't install on this device&goto:eof
call:permissionOp %1 revoke %2
echo package:%2 all permission was revoked.
goto:eof

:grantPermission
rem param deviceId
rem param packageName
call:_isInstall %1 %2
if not "!_isInstall!"=="TRUE" echo %2 isn't install on this device&goto:eof
call:permissionOp %1 grant %2
echo package:%2 all permission was granted.
goto:eof

:permissionOp
rem param deviceId
rem param op
rem param packageName
adb -s %1 shell pm %2 %3 android.permission.CAMERA 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.READ_CALENDAR 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.WRITE_CALENDAR 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.READ_CONTACTS 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.WRITE_CONTACTS 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.GET_ACCOUNTS 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.ACCESS_FINE_LOCATION 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.ACCESS_COARSE_LOCATION 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.RECORD_AUDIO 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.READ_PHONE_STATE 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.CALL_PHONE 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.READ_CALL_LOG 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.WRITE_CALL_LOG 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.ADD_VOICEMAIL 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.USE_SIP 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.PROCESS_OUTGOING_CALLS 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.BODY_SENSORS 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.SEND_SMS 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.RECEIVE_SMS 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.READ_SMS 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.RECEIVE_WAP_PUSH 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.RECEIVE_MMS 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.READ_EXTERNAL_STORAGE 1>nul 2>nul
adb -s %1 shell pm %2 %3 android.permission.WRITE_EXTERNAL_STORAGE 1>nul 2>nul
goto:eof

:isInstall
rem param deviceId
rem param packageName
call:_isInstall %1 %2
if not "!_isInstall!"=="TRUE" echo package:%2 isn't install on this device&goto:eof
if "!_isInstall!"=="TRUE" echo package:%2 is install on this device&goto:eof
goto:eof

:_isInstall
rem param deviceId
rem param packageName
set _isInstall=FALSE
adb -s %1 shell pm list package %2>_isInstall.tmp
for /f %%i in (_isInstall.tmp) do set _isInstall=TRUE
del _isInstall.tmp
goto:eof

:launcherComponent
rem param deviceId
rem param packageName
if exist %2.launcherComponent goto:eof
call:log get launcher Component by activity manager.
call:_isInstall %1 %2
if not "!_isInstall!"=="TRUE" echo %2 isn't install on this device&goto:eof
set LANCOMSEC=
adb -s %1 shell dumpsys activity -p %2|findstr %2|findstr android.intent.action.MAIN>launcherComponent.tmp
for /f "tokens=6" %%i in (launcherComponent.tmp) do set LANCOMSEC=%%i&set LANCOMSEC=!LANCOMSEC:~4!
del launcherComponent.tmp
if not "!LANCOMSEC!"=="" (echo !LANCOMSEC!>%2.launcherComponent) else (call:launcherComponent2 %1 %2)
if not exist %2.launcherComponent echo get %2 launcher component with error.
goto:eof

:launcherComponent2
rem param deviceId
rem param packageName
call:log get launcher Component by package manager.
call:_isInstall %1 %2
if not "!_isInstall!"=="TRUE" echo %2 isn't install on this device&goto:eof
adb -s %1 shell dumpsys package %2|findstr /n ".">%2.txt
findstr "android.intent.action.MAIN" %2.txt>mark.txt 
for /f "skip=1" %%i in (mark.txt) do set lineNumber=%%i&set lineNumber=!lineNumber:~,-1!&set /a lineNumber=!lineNumber!-1&call:setlauncherComponentVAL !lineNumber!:
call:log !COMSEC0! !COMSEC1! !COMSEC2! !COMSEC3! !COMSEC4! !COMSEC5! !COMSEC6! !COMSEC7! !COMSEC8! !COMSEC9! 
for /f "tokens=1,3" %%i in (%2.txt) do call:setlauncherComponentResult %%i %%j %2
if "!DEBUG!"=="TRUE" type %2.launcherComponent
goto:eof 

:setlauncherComponentVAL
::param component
if "!COMSEC0!"=="" set COMSEC0=%1&goto:eof
if "!COMSEC1!"=="" set COMSEC1=%1&goto:eof
if "!COMSEC2!"=="" set COMSEC2=%1&goto:eof
if "!COMSEC3!"=="" set COMSEC3=%1&goto:eof
if "!COMSEC4!"=="" set COMSEC4=%1&goto:eof
if "!COMSEC5!"=="" set COMSEC5=%1&goto:eof
if "!COMSEC6!"=="" set COMSEC6=%1&goto:eof
if "!COMSEC7!"=="" set COMSEC7=%1&goto:eof
if "!COMSEC8!"=="" set COMSEC8=%1&goto:eof
if "!COMSEC9!"=="" set COMSEC9=%1&goto:eof
goto:eof

:setlauncherComponentResult
::param linenumberMark
::param componentInfo
::param packName
if "!COMSEC0!"=="%1" echo %2>%3.launcherComponent
if "!COMSEC1!"=="%1" echo %2>>%3.launcherComponent
if "!COMSEC2!"=="%1" echo %2>>%3.launcherComponent
if "!COMSEC3!"=="%1" echo %2>>%3.launcherComponent
if "!COMSEC4!"=="%1" echo %2>>%3.launcherComponent
if "!COMSEC5!"=="%1" echo %2>>%3.launcherComponent
if "!COMSEC6!"=="%1" echo %2>>%3.launcherComponent
if "!COMSEC7!"=="%1" echo %2>>%3.launcherComponent
if "!COMSEC8!"=="%1" echo %2>>%3.launcherComponent
if "!COMSEC9!"=="%1" echo %2>>%3.launcherComponent
goto:eof

:deviceInfo
::param deviceNo
call:getManufacturer %1&call:getModel %1
echo [!time!] !manufacturer! !model!
goto:eof

:getManufacturer
:: param deviceNo
if "%1"=="" echo please input serialNo.&goto :eof
adb -s %1 shell getprop ro.product.manufacturer>manufacturer.txt
for /f "delims=" %%i in (manufacturer.txt) do set manufacturer=%%i
del manufacturer.txt
goto :eof

:getModel
:: param deviceNo
if "%1"=="" echo please input serialNo.&goto :eof
adb -s %1 shell getprop ro.product.model>model.txt
for /f "delims=" %%i in (model.txt) do set model=%%i
del model.txt
goto :eof


:log 
if "!DEBUG!"=="TRUE" echo,%1 %2 %3 %4 %5 %6 %7 %8 %9
goto:eof



:help
echo adbIdea [cmd] [what] packageName
echo,
echo   cmd nay be one of:
echo      [start] start app
echo      [startWeb] open url by web app.
echo      [kill] kill app
echo      [restart] restart app
echo      [restartAdb] restart adb.
echo      [clear] clear app data 
echo      [cleartAndRestart] clear app data then start it
echo      [details] skip to app details
echo      [topInfo] print the current app component face to user.
echo      [taskInfo] print Recent task running on the phone.
echo      [launcherComponent] show the launcher component 
echo      [uninstall] uninstall the app.
echo      [revokePermission] revoke permission by app apply.
echo      [grantPermission] grant permission by app apply.
echo   what may
goto:eof
